//
//  VideoController.m
//  WowDara
//
//  Created by Siroj Chongkolwanont on 4/12/56 BE.
//  Copyright (c) 2556 Siroj Chongkolwanont. All rights reserved.
//

#import "VideoController.h"
#import "Video.h"
#import "VideoChapter.h"

@interface VideoController ()

@end

@implementation VideoController

NSMutableArray *videoArray;

Video* currentVideo;

id jsonObjectFromUrlString(NSString *urlString);


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = [videoArray count];
    
    return row;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    NSInteger row = indexPath.row;
        
   currentVideo = [videoArray objectAtIndex:row];

}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    
    currentVideo = [videoArray objectAtIndex:row];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"VideoCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSInteger row = indexPath.row;
    
    Video* video = videoArray[row];
    
    UILabel *videoLabel = (UILabel *)[cell viewWithTag:101];
    videoLabel.text = video.name;
    
    UILabel *videoType = (UILabel *)[cell viewWithTag:104];
    videoType.text = video.video_type;
    
    UILabel *videoOnAir = (UILabel *)[cell viewWithTag:106];
    videoOnAir.text = video.video_date_time;
    
    UILabel *videoUpdate = (UILabel *)[cell viewWithTag:107];
    videoUpdate.text = video.video_last_update;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // Here we use the new provided setImageWithURL: method to load the web image
    
    NSString *url_picture = @"http://119.59.127.95/~wowdara/";
    
    url_picture = [url_picture stringByAppendingString:video.thumbs_url];
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_picture]];
    
    
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    
    recipeImageView.image = [UIImage imageWithData:imageData];
    
    return cell;
}


- (void)load_video
{
    videoArray = [[NSMutableArray alloc] init];
    
    NSDictionary *publicTimeline =  jsonObjectFromUrlString(@"http://119.59.127.95/~wowdara/android/getVideo.php");
    
    
    for(NSString *item in [publicTimeline allKeys]) {
        
        if([item isEqual: @"video"] )
        {
            NSArray *allVideo = [publicTimeline valueForKey:item];
            
            for(NSDictionary *dara in allVideo )
            {
                Video* video_obj = [[Video alloc] init];
                video_obj.id    = [dara valueForKey:@"video_id"];
                video_obj.name = [dara valueForKey:@"video_name"];
                video_obj.thumbs_url  = [dara valueForKey:@"thumbs_url"];
                video_obj.images_url  = [dara valueForKey:@"images_url"];
                video_obj.video_date_time  = [dara valueForKey:@"video_date_time"];
                video_obj.video_last_update  = [dara valueForKey:@"video_last_update"];
                video_obj.video_type  = [dara valueForKey:@"video_type"];
                video_obj.video_chapter  = [dara valueForKey:@"video_chapter"];
                [videoArray addObject:video_obj];
            }
            
        }
    }
}

- (void)viewDidLoad
{
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"nav_bg.png"]
                                                  forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"toolbar_logo.png"]]];
    
    
    UIButton *button1 = [[UIButton alloc] init];
    button1.frame= CGRectMake(0,0,43,32);
    [button1 setBackgroundImage:[UIImage imageNamed: @"nav_contact.png"] forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
    
    [self load_video];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    //if([segue.identifier isEqualToString:@"ToSummary"])
    {
        VideoChapter *p = (VideoChapter*)segue.destinationViewController;
        p.video = currentVideo;
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
